#include <iostream>
#include <vector>
#include <string>
#include <unistd.h>
#include <cstdio>
#include <sys/types.h>
#include <sys/wait.h>
#include <functional>
#include "task.h"

typedef std::function<void()> task_t;
// using task_t = std::function<void()>;

void LoadTask(std::vector<task_t> &tasks)
{
    tasks.push_back(PrintLog);
    tasks.push_back(DownLoag);
    tasks.push_back(BackUp);
}

int main()
{
    // 任务列表
    std::vector<task_t> tasks;
    LoadTask(tasks); // 加载任务

    pid_t id = fork();
    if (id == 0) // 子进程
    {
        while (true)
        {
            printf("我是子进程,pid: %d\n", getpid());
            sleep(1);
        }
        exit(0);
    }
    // 父进程
    while (true)
    {
        sleep(1);
        pid_t rid = waitpid(id, nullptr, WNOHANG);
        if (rid > 0)
        {
            printf("等待子进程%d成功\n", rid);
            break;
        }
        else if (rid < 0)
        {
            printf("等待子进程失败\n");
            break;
        }
        else
        {
            printf("子进程尚未退出\n");
            // 父进程做自己的事情
            for (auto &task : tasks)
            {
                task();
            }
        }
    }
}